package com.echo.tagparse.face;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import com.echo.tagparse.face.stemmer.Stemmer;


public class StemFace {
	
	Stemmer s = null;
	BufferedWriter writer = null;
	
	 public StemFace(){
		 s = new Stemmer();
	 }
	 
	 public void StemStr(String tempFile){
		 String stemFile = "stemFile.txt";
		 try{
			 writer = new BufferedWriter(new FileWriter("c://"+stemFile));
			 char[] w = new char[501];
			   try
			   {
			      FileInputStream in = new FileInputStream(tempFile);
			      try
			      { while(true)
	
			        {  int ch = in.read();
			           if (Character.isLetter((char) ch))
			           {
			              int j = 0;
			              while(true)
			              {  ch = Character.toLowerCase((char) ch);
			                 w[j] = (char) ch;
			                 if (j < 500) j++;
			                 ch = in.read();
			                 if (!Character.isLetter((char) ch))
			                 {
			                    /* to test add(char ch) */
			                    for (int c = 0; c < j; c++) s.add(w[c]);
	
			                    /* or, to test add(char[] w, int j) */
			                    /* s.add(w, j); */
	
			                    s.stem();
			                    {  String u;
	
			                       /* and now, to test toString() : */
			                       u = s.toString();
	
			                       /* to test getResultBuffer(), getResultLength() : */
			                       /* u = new String(s.getResultBuffer(), 0, s.getResultLength()); */
			                       writer.write(u);
			                       System.out.print(u);
			                    }
			                    break;
			                 }
			              }
			           }
			           if (ch < 0) break;
			           writer.write((char)ch);
			           System.out.print((char)ch);
			        }
			      	writer.flush();
			      }
			      catch (IOException e)
			      {  System.out.println("error reading " + tempFile);
			      }
			   }
			   catch (FileNotFoundException e)
			   {  System.out.println("file " + tempFile + " not found");
			   }
		 }catch(IOException e){
			 e.printStackTrace();
		 }
	 }
}
